//
//  NSDate+dateWithString.m
//  muefb
//
//  Created by 徐 洋 on 12-7-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "NSDate+dateWithString.h"

@implementation NSDate (dateWithString)

+ (NSDate *)dateWithString:(NSString *)dateString format:(NSDateFormatter *)dateFormatter
{
    if ( [dateString isKindOfClass:[NSNull class] ]|| [dateString isEqualToString:@""]) {
        return nil;
    }
    
    NSDate * result =nil;
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.sssZZZZZ"];

    result = [dateFormatter dateFromString:dateString];
    if (result == nil) {
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        result = [dateFormatter dateFromString:dateString];
    }
    if (result == nil) {
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
        result = [dateFormatter dateFromString:dateString];
    }
    return result;
}

+ (NSDate *) dateWithString:(NSString *)dateString
{

     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSDate * result = [NSDate dateWithString:dateString format:dateFormatter];
    
    [dateFormatter release];
    return result;
}

+ (NSDate *) dateWithUtcString:(NSString *)dateString;
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    
    NSDate * result = [NSDate dateWithString:dateString format:dateFormatter];

    [dateFormatter release];
    
    return result;
}

@end
